<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\models\system;

use yii\base\InvalidConfigException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "sys_admin".
 *
 * @property int $id
 * @property string $user_name 登录名
 * @property string $nick_name 人员姓名
 * @property string|null $user_type 用户类型
 * @property string|null $password 登录密码
 * @property string|null $avatar 头像
 * @property string|null $email 邮箱
 * @property string|null $phone 电话
 * @property string|null $sex 性别
 * @property string|null $status 状态
 * @property string|null $remark 备注
 * @property int|null $create_time 创建时间
 * @property int|null $update_time 更新时间
 * @property-read ?Dept $deptOne
 * @property-read ?AdminDept[] $deptLink
 * @property-read ?AdminRole[] $roleLink
 * @property-read ?AdminPos[] $posLink
 * @property-read Role[]|null $roleList
 * @property-read Position[]|null $posList
 * @property-read Dept[]|null $deptList
 */
class Admin extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName(): string
    {
        return 'sys_admin';
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios(): array
    {
        $scenarios = parent::scenarios();
        $scenarios['edit'] = ['nick_name', 'status', 'dept_id', 'remark'];
        $scenarios['profile'] = ['nick_name', 'email', 'phone', 'sex'];
        return $scenarios;
    }

    /**
     * {@inheritdoc}
     */
    public function rules(): array
    {
        return [
            [['user_name'], 'required'],
            [['phone', 'email', 'sex'], 'required', 'on' => 'profile'],

            [['nick_name', 'user_type', 'avatar', 'email', 'phone', 'remark'], 'default', 'value' => ''],
            ['sex', 'default', 'value' => '0'],
            ['status', 'default', 'value' => '1'],

            [['user_name', 'nick_name', 'user_type', 'password', 'avatar', 'email', 'phone', 'sex', 'status', 'remark'], 'trim'],

            [['user_name', 'phone', 'user_type'], 'string', 'max' => 32],
            [['nick_name', 'email'], 'string', 'max' => 64],
            [['avatar', 'remark'], 'string', 'max' => 250],
            [['password'], 'string', 'max' => 20, 'min' => 6],
            ['status', 'in', 'range' => ['0', '1']],
            ['sex', 'in', 'range' => ['0', '1', '2', '3']],

            [['user_name'], 'unique'],

            [['create_time', 'update_time'], 'safe'],
        ];
    }

    /**
     * toArray时，返回字段传力
     * @return array
     */
    public function fields(): array
    {
        $fields = parent::fields();
        unset($fields['password']);
        return $fields;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels(): array
    {
        return [
            'id' => '人员ID',
            'user_name' => '登录名',
            'nick_name' => '人员姓名',
            'user_type' => '用户类型',
            'password' => '登录密码',
            'avatar' => '头像',
            'email' => '邮箱',
            'phone' => '手机号码',
            'sex' => '性别',
            'status' => '状态',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function afterDelete()
    {
        if ($this->id) {
            AdminRole::deleteAll(['admin_id' => $this->id]);//删除管理员角色
            AdminPos::deleteAll(['admin_id' => $this->id]); //删除管理员岗位
            AdminDept::deleteAll(['admin_id' => $this->id]); //删除管理员部门
        }
        parent::afterDelete();
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
                'value' => function () {
                    return time();
                }
            ]
        ];
    }

    public static function encryptPassword($pwd): string
    {
        return md5($pwd . '__b5net');
    }

    public function getDeptOne(): ?ActiveQuery
    {
        try {
            return $this->hasOne(Dept::class, ['id' => 'dept_id'])->viaTable(AdminDept::tableName(), ['admin_id' => 'id']);
        } catch (InvalidConfigException $e) {
            return null;
        }
    }

    public function getDeptLink(): ?ActiveQuery
    {
        return $this->hasMany(AdminDept::class, ['admin_id' => 'id']);
    }

    public function getDeptList(): ?ActiveQuery
    {
        return $this->hasMany(Dept::class, ['id' => 'dept_id'])->via('deptLink');
    }

    public function getRoleLink(): ?ActiveQuery
    {
        return $this->hasMany(AdminRole::class, ['admin_id' => 'id']);
    }

    public function getRoleList(): ?ActiveQuery
    {
        return $this->hasMany(Role::class, ['id' => 'role_id'])->via('roleLink');
    }

    public function getPosLink(): ?ActiveQuery
    {
        return $this->hasMany(AdminPos::class, ['admin_id' => 'id']);
    }

    public function getPosList(): ?ActiveQuery
    {
        return $this->hasMany(Position::class, ['id' => 'pos_id'])->via('posLink');
    }
}
